var index=1;
require.config({
    paths: {
        'columntoggle': '../addons/workteam/js/columntoggle',
    }
});
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme','columntoggle','selectpage'],
    function ($, undefined, Backend, Table, Form,Echarts, undefined,Columntoggle,selectpage ) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/project/index' + location.search,
                    add_url: 'workteam/project/add',
                    edit_url: 'workteam/project/edit',
                    del_url: 'workteam/project/del',
                    multi_url: 'workteam/project/multi',
                    table: 'workteam_project',
                }
            });

            var table = $("#workteam_project");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: '',
	            pageSize: 50,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true},
                        {
                            field: 'scene_id',
                            title: __('场景'),
                            visible: false,
                            formatter: function (v, r) {
                                return Config.scene_list[v];
                            }, searchList: Config.scene_list
                        },
                        {field: 'number', title: __('项目编号'),operate: "LIKE",sortable: true},


                        {field: 'name', title: __('项目名称'),align: 'left',operate: "LIKE"
                            ,customField: 'level'
                            ,formatter: function (value, row, index) {
                                //判断是否逾期
                                var newtime = Math.round(new Date().getTime()/1000).toString();
                                if (newtime>row.end_time&&$.inArray(row.status,[2,3])==-1 )
                                    value='<span class="label label-danger">逾</span>'+value;
                                return value +
                                    Controller.api.level(row['level'], row, index,this);
                            }},

                        {
                            field: 'jobs',
                            title: __('作业内容'),
                            operate: 'find_in_set',
                            addclass: 'selectpicker',
                            // extend: "multiple",
                            visible: true,
                            searchList:jobs_obj,
                            formatter: Table.api.formatter.flag
                        },

                        {field: 'customer_name', title: __('客户名称'),operate: "LIKE",
                            align:'left', customField:'tags', class:'tabletd',
	                        formatter:function (value, row, index) {
                                return '<a href="javascript:void(0);" data-url="facrm/customer/index/detail/ids/' + row.customer_id + '" data-area="[\'98%\',\'98%\']" class="btn-addtabs" data-toggle="tooltip"  data-original-title="'+ value +'"  data-title="'+ value+'">'+ value +'</a>';
							}
                        },
                        {field: 'remark', title: __('备注'),operate: "LIKE",},
             
                        {field: 'project_admin_id', title: __('项目经理'),addclass: 'selectpage',visible:true,
                            extend: "data-source='workteam/common/selectpage/model/admin/type/all' data-field='nickname'",formatter: function (value, row, index) {
                                 if(!row.padmin) return '-';
                                return row.padmin.nickname;
                        }},
						{field: 'sales_admin_id', title: __('业务经理'),addclass: 'selectpage',visible:true,
						extend: "data-source='workteam/common/selectpage/model/admin/type/all' data-field='nickname'",formatter: function (value, row, index) {
                                 if(!row.sadmin) return '-';
                                return row.sadmin.nickname;
                        }},

                        {field: 'plan_hours', title: __('计划工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'actual_hours', title: __('实际工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'task_count', title: __('任务总量'), operate: 'BETWEEN', sortable: true},
                        {field: 'task_complate', title: __('完成数量'), operate: 'BETWEEN', sortable: true},
                        {
                            field: 'progress',
                            title: __('进度'),
                            operate: false,
                            formatter: function (value, row, index) {
                                var plan_hours_p='0%';
                                row.plan_hours= parseFloat(row.plan_hours);
                                row.task_hours= parseFloat(row.task_hours);
                                if (row.plan_hours<row.task_hours){
                                    row.plan_hours=row.task_hours;
                                }
                                plan_hours_p=(row.actual_hours/row.plan_hours)*100;
                                plan_hours_p=plan_hours_p.toFixed(2)
                                var str ='<div class="progress" ><div class="progress-bar" style="width:'+plan_hours_p+'%;">'+plan_hours_p+'%</div></div>';
                                return str;
                            }
                        },
                        {field: 'status', title: __('Status'), searchList: Config.statusList, formatter: Table.api.formatter.status,sortable: true},
                        {field: 'level', title: __('优先级'), searchList: Config.levelList,visible:false, formatter: Table.api.formatter.status},
                        {field: 'start_time', title: __('开始日期'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'end_time', title: __('截止日期'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'create_time', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'update_time', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'complete_time', title: __('完成时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'complete',
                                    text: '',
                                    title: __('完成项目'),
                                    icon: 'fa fa-check-circle',
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    url: 'workteam/project/complete',
                                    confirm: __("确认完成项目？"),
                                    hidden:function (row) {
                                        if (row.status!=1) return true;
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'guidang',
                                    text: '',
                                    title: __('归档项目'),
                                    icon: 'fa fa-floppy-o',
                                    confirm: __("归档之后不能再对项目进行操作，确认归档项目？"),
                                    classname: 'btn btn-xs btn-danger  btn-ajax',
                                    url: 'workteam/project/guidang',
                                    hidden:function (row) {
                                        if (row.status!=2) return true;
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '详情',
                                    title:function (row) {
                                        return __('项目:')+row.name;
                                    },
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: 'workteam/project/detail',

                                },
                                {
                                    name: 'task',
                                    text: '',
                                    title: __('添加任务'),
                                    icon: 'fa fa-plus',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'workteam/task/add',
                                    hidden:function (row) {
                                        if ($.inArray(row.status,[1,0])==-1) return true;
                                    },
                                },

                            ]}
                    ]
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("workteam_project")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("workteam_project","myTabContent")
                },
                exportOptions: {
                    htmlContent: false,
                    onCellHtmlData: function (cell, row, col, data){//处理导出内容,自定义某一行、某一列、某个单元格的内容
                        return data;
                    },
                },

                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var complete_status;
            //提交完成项目
            $(document).on('click', ".complete_sub", function () {
                complete_status=$(this).data('status');

                $(".btn-complete_status").html(Config.statusList[complete_status+'']+__("项目"));
            });
            //提交批量归档和完成项目
            $(document).on('click', ".btn-complete_status", function () {

                var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                        url:'workteam/project/multi',
                        data:{params:'status='+complete_status,ids:ids}
                    }
                    , function () {
                        $(".btn-refresh").trigger("click");
                        $('#modelProduct').modal('hide');
                    }, function () {
                        alert(__("提交错误"));
                    });
            });
            $(".btn-print").on("click", function(){
                var ids = Table.api.selectedids(table);
                Fast.api.open("workteam/project/print/ids/"+ids);
            });


            Controller.api.bindevent();
        },
        customer: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/project/index/customer_id/' + Config.customer_id + '/' + location.search,
                    add_url: 'workteam/project/add',
                    edit_url: 'workteam/project/edit',
                    del_url: 'workteam/project/del',
                    multi_url: 'workteam/project/multi',
                    table: 'workteam_project',
                }
            });

            var table = $("#workteam_project");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: '',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable: true},
                        {
                            field: 'scene_id',
                            title: __('场景'),
                            visible: false,
                            formatter: function (v, r) {
                                return Config.scene_list[v];
                            }, searchList: Config.scene_list
                        },
                        {
                            field: 'customer_id',
                            title: __('场景'),
                            // visible: false,
                        },
                        {field: 'number', title: __('项目编号'),operate: "LIKE",sortable: true},


                        {field: 'name', title: __('项目名称'),align: 'left',operate: "LIKE"
                            ,customField: 'level'
                            ,formatter: function (value, row, index) {
                                //判断是否逾期
                                var newtime = Math.round(new Date().getTime()/1000).toString();
                                if (newtime>row.end_time&&$.inArray(row.status,[2,3])==-1 )
                                    value='<span class="label label-danger">逾</span>'+value;
                                return value +
                                    Controller.api.level(row['level'], row, index,this);
                            }},

                        {
                            field: 'jobs',
                            title: __('作业内容'),
                            operate: 'find_in_set',
                            addclass: 'selectpicker',
                            extend: "multiple",
                            visible: true,
                            searchList:jobs_obj,
                            formatter: Table.api.formatter.flag
                        },

                        {field: 'customer_name', title: __('客户名称'),operate: false,
                            align:'left', customField:'tags', class:'tabletd',
	                        formatter:function (value, row, index) {
                                return '<a href="javascript:void(0);" data-url="facrm/customer/index/detail/ids/' + row.customer_id + '" data-area="[\'98%\',\'98%\']" class="btn-addtabs" data-toggle="tooltip"  data-original-title="'+ value +'"  data-title="'+ value+'">'+ value +'</a>';
							}
                        },
                        {field: 'remark', title: __('备注'),operate: "LIKE",},

                        {field: 'project_admin_id', title: __('项目经理'),addclass: 'selectpage',visible:true,
                            extend: "data-source='workteam/common/selectpage/model/admin/type/all' data-field='nickname'",formatter: function (value, row, index) {
                                 if(!row.padmin) return '-';
                                return row.padmin.nickname;
                        }},
						{field: 'sales_admin_id', title: __('业务经理'),addclass: 'selectpage',visible:true,
						extend: "data-source='workteam/common/selectpage/model/admin/type/all' data-field='nickname'",formatter: function (value, row, index) {
                                 if(!row.sadmin) return '-';
                                return row.sadmin.nickname;
                        }},

                        {field: 'plan_hours', title: __('计划工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'actual_hours', title: __('实际工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'task_count', title: __('任务总量'), operate: 'BETWEEN', sortable: true},
                        {field: 'task_complate', title: __('完成数量'), operate: 'BETWEEN', sortable: true},
                        {
                            field: 'progress',
                            title: __('进度'),
                            operate: false,
                            formatter: function (value, row, index) {
                                var plan_hours_p='0%';
                                row.plan_hours= parseFloat(row.plan_hours);
                                row.task_hours= parseFloat(row.task_hours);
                                if (row.plan_hours<row.task_hours){
                                    row.plan_hours=row.task_hours;
                                }
                                plan_hours_p=(row.actual_hours/row.plan_hours)*100;
                                plan_hours_p=plan_hours_p.toFixed(2)
                                var str ='<div class="progress" ><div class="progress-bar" style="width:'+plan_hours_p+'%;">'+plan_hours_p+'%</div></div>';
                                return str;
                            }
                        },
                        {field: 'status', title: __('Status'), searchList: Config.statusList, formatter: Table.api.formatter.status,sortable: true},
                        {field: 'level', title: __('优先级'), searchList: Config.levelList,visible:false, formatter: Table.api.formatter.status},
                        {field: 'start_time', title: __('开始日期'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'end_time', title: __('截止日期'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'create_time', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'update_time', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'complete_time', title: __('完成时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Controller.api.formatter.datetime,sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'complete',
                                    text: '',
                                    title: __('完成项目'),
                                    icon: 'fa fa-check-circle',
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    url: 'workteam/project/complete',
                                    confirm: __("确认完成项目？"),
                                    hidden:function (row) {
                                        if (row.status!=1) return true;
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'guidang',
                                    text: '',
                                    title: __('归档项目'),
                                    icon: 'fa fa-floppy-o',
                                    confirm: __("归档之后不能再对项目进行操作，确认归档项目？"),
                                    classname: 'btn btn-xs btn-danger  btn-ajax',
                                    url: 'workteam/project/guidang',
                                    hidden:function (row) {
                                        if (row.status!=2) return true;
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '详情',
                                    title:function (row) {
                                        return __('项目:')+row.name;
                                    },
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: 'workteam/project/detail',

                                },
                                {
                                    name: 'task',
                                    text: '',
                                    title: __('添加任务'),
                                    icon: 'fa fa-plus',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'workteam/task/add',
                                    hidden:function (row) {
                                        if ($.inArray(row.status,[1,0])==-1) return true;
                                    },
                                },

                            ]}
                    ]
                ],
	            queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
					//这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.customer_id = Config.customer_id;
                    op.customer_id = "=";
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("workteam_project")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("workteam_project","myTabContent")
                },
                exportOptions: {
                    htmlContent: false,
                    onCellHtmlData: function (cell, row, col, data){//处理导出内容,自定义某一行、某一列、某个单元格的内容
                        return data;
                    },
                },

                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var complete_status;
            //提交完成项目
            $(document).on('click', ".complete_sub", function () {
                complete_status=$(this).data('status');

                $(".btn-complete_status").html(Config.statusList[complete_status+'']+__("项目"));
            });
            //提交批量归档和完成项目
            $(document).on('click', ".btn-complete_status", function () {

                var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                        url:'workteam/project/multi',
                        data:{params:'status='+complete_status,ids:ids}
                    }
                    , function () {
                        $(".btn-refresh").trigger("click");
                        $('#modelProduct').modal('hide');
                    }, function () {
                        alert(__("提交错误"));
                    });
            });
            $(".btn-print").on("click", function(){
                var ids = Table.api.selectedids(table);
                Fast.api.open("workteam/project/print/ids/"+ids);
            });


            Controller.api.bindevent();
        },
        recyclebin: function () {

        },
        add: function () {
            Controller.api.bindevent();
            $(function () {
                setnums();
            });

            $(document).on('click', '.addRow',function(){
                Controller.api.addRow();
            });
            $(document).on('click', '.delRow',function(){
                Controller.api.delRow($(this));
            });
        },
        edit: function () {
            Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/project/fileslist/project_id/'+project_id,
                    add_url: 'workteam/project/filesadd/project_id/'+project_id,
                    edit_url: '',
                    del_url: 'workteam/project/filesdel/project_id/'+project_id,
                    multi_url: '',
                    table: ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('上传人'), operate: false},
                        {field: 'admin_id', title: __('上传人'), visible: false, addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'preview', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'filename', title: __('文件名称'), operate: false},
                        {field: 'url', title: __('文件地址'), formatter: Controller.api.formatter.url, visible: false},
                        {
                            field: 'create_time',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //上传附件
            $("#c-third").on("change",function () {
                var url=$(this).val();
                if (url){
                    $(this).val("");
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.add_url,
                        data: {url:url}
                    },function (data) {
                        table.bootstrapTable('refresh', {pageNumber: 1});
                    });
                }
            });


        },
        addlog:function(){
            Controller.api.bindevent();
            //通知
            $("#c-notice").on("click",function () {
                $(".admin-ids-div").toggle();
            });
        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/project/tasklist/project_id/'+project_id,
                    add_url: 'workteam/task/add/project_id/'+project_id,
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: 'workteam_task',
                }
            });

            var tabletask = $("#tabletask");
            // 初始化表格
            tabletask.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 10,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: true,
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    var activeobj= $('#tab-my-task .active a[data-toggle="tab"]');
                    params.filter= JSON.stringify($(activeobj).data('filter'));
                    params.op= JSON.stringify(activeobj.data('op'));
                    return params
                },
                columns: [
                    [
                        {field: 'name', title: __('任务'),align: 'left'
                            ,customField: 'level'
                            ,formatter: function (value, row, index) {
                                //判断是否逾期
                                var newtime = Math.round(new Date().getTime()/1000).toString();
                                if (newtime>row.end_time&&row.status!=2)
                                    value='<span class="label label-danger">逾</span>'+value;
                                return value +
                                    Controller.api.level(row['level'], row, index,this);
                            }},
                        {field: 'ptask.name', title: __('父任务'), operate:false, sortable: false},
                        {field: 'admin.nickname', title: __('负责'), operate:false, sortable: false},
                        {field: 'plan_hours', title: __('计划'), operate: 'BETWEEN', sortable: true},
                        {field: 'get_hours', title: __('可得'), operate: 'BETWEEN', sortable: true},
                        {field: 'status', title: __('状态'), searchList: Config.statusList, formatter: Table.api.formatter.status},
                        {
                            field: 'progress',
                            title: __('进度'),
                            editable: true,
                            operate: false,
                            formatter: function (value, row, index) {
                                var str =value+'%';
                                return str;
                            }
                        },
                        {field: 'start_time', title: __('开始'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime,sortable: true},
                        {field: 'end_time', title: __('截止'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime,sortable: true},

                        {field: 'operate', title: __('Operate'), table: tabletask, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'start',
                                    text: '',
                                    title: __('开始任务'),
                                    icon: 'fa fa-play-circle-o',
                                    confirm: __("确认开始任务"),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    url: 'workteam/task/start',
                                    hidden:function (row) {
                                        if (row.status!=0) return true;
                                    },
                                    success:function () {
                                        tabletask.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'complete',
                                    text: '',
                                    title: __('完成任务'),
                                    icon: 'fa fa-check-circle',
                                    // classname: 'btn btn-xs btn-info btn-ajax',
                                    // confirm: __("确认完成任务？完成之后不能再操作"),
	                                classname: 'btn btn-xs btn-success btn-dialog',
	                                url: 'workteam/task/complete',
                                    hidden:function (row) {
                                        if (row.status!=1) return true;
                                    },
                                    success:function () {
                                        tabletask.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'addchild',
                                    text: '',
                                    title: __('添加子任务'),
                                    icon: 'fa fa-plus',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'workteam/task/addchild',
                                    hidden:function (row) {
                                        if (row.status==2) return true;
                                    },
                                    callback: function (data) {
                                        tabletask.bootstrapTable('refresh');
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: '',
                                    title: __('任务详情'),
                                    icon: 'fa fa-info-circle',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'workteam/task/detail',
                                },
                            ]}
                    ]
                ],
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
            });

            // 为表格绑定事件
            Table.api.bindevent(tabletask);



            // 绑定TAB事件
            $('#tab-my-task a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
				var idnum = $(this).data('idnum');
				$('.task-box').removeClass('active');
				if(typeof idnum=="undefined" || idnum==""){
					$('#one').addClass('active');
					tabletask.bootstrapTable('refresh', {pageNumber: 1});
				}else{
					$('#one'+idnum).addClass('active');
					if(idnum == 1){
						yjt_echats.resize();
					}
				}
                return false;
            });
            //添加任务
            $(".btn-addtask").on("click",function () {
                var url=$(this).data('url');
                Fast.api.open(url,$(this).attr('title'),{callback: function () {
                        tabletask.bootstrapTable('refresh');
                    }});
            });

			var table_customer_log = $('#table-customer-log');
			table_customer_log.bootstrapTable({
                url: 'service/log/index/project_id/'+project_id ,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 10,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: true,
                columns: [
                    [
                        {field: 'task.name', title: __('任务名称'), table: table_customer_log, class: 'autocontent',
	                        formatter: function (value, row, index) {
								if (!value) return '-';
								return value;
							}
						},
                        {field: 'service_client_text', title: __('服务对象'), table: table_customer_log, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'service_time', title: __('服务时间'), addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'content_text', title: __('可公开内容'), table: table_customer_log, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'secret_text', title: __('隐私内容'), table: table_customer_log, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'admin.nickname', title: __('创建人')},
                        {field: 'operate', title: __('Operate'), table: table_customer_log, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
							buttons: [
								{
		                            name: 'edit1',
		                            text: '',
		                            title: '修改',
		                            icon: 'fa fa-pencil',
		                            classname: 'btn btn-xs btn-success  btn-dialog',
		                            url: 'service/log/edit',

		                        },
		                        {
									name: 'del1',
		                            text: '',
		                            title: '删除',
		                            confirm:"确认删除客情记录？",
		                            icon: 'fa fa-trash',
		                            classname: 'btn btn-xs btn-danger  btn-ajax',
		                            url: 'service/log/del',
		                            success:function(data,ret){
		                                table_customer_log.bootstrapTable('refresh');
		                            },
		                        },
							],
						}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table_customer_log);

            //添加客情记录
            $(".btn-add-customer-log").on("click",function () {
                var url=$(this).data('url');
                Fast.api.open(url,$(this).attr('title'),{callback: function () {
                        table_customer_log.bootstrapTable('refresh');
                    }});
            });


            var tablelog = $("#tablelog");
            // 初始化表格
            tablelog.bootstrapTable({
                url: 'workteam/log/index/project_id/'+project_id ,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 10,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: true,
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    var activeobj= $('#tab-log .active a[data-toggle="tab"]');
                    params.filter= JSON.stringify($(activeobj).data('filter'));
                    params.op= JSON.stringify(activeobj.data('op'));
                    return params
                },
                columns: [
                    {field: 'id', title: 'ID', operate: false},
                    {field: 'username', title: __('Username'), formatter: Table.api.formatter.search},
                    {field: 'type', title: __('类型'), operate: false,formatter:function (val,row) {
                            return row.type_text;
                        } },
                    // {field: 'create_type', title: __('录入方式'),custom:{1:'danger',0:'info'},formatter:Table.api.formatter.flag,searchList:Config.createTypeList},
                    {field: 'title', title: __('Title'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                    {field: 'content_text', title: __('内容'),align: 'left', operate: false,formatter:function (val) {
                               return '<div class="logcontent" data-toggle="tooltip" data-title="'+val+'">'+val+'</div>';
                        } },
                    {field: 'create_time', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    {field: 'operate', title: __('Operate'), table: tablelog,
                        events: Table.api.events.operate,
                        buttons: [{
                            name: 'detail',
                            text: __('Detail'),
                            icon: 'fa fa-list',
                            classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                            url: 'workteam/log/detail',
                        }],
                        formatter: Table.api.formatter.operate
                    }
                ],
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
            });
            // 为表格绑定事件
            Table.api.bindevent(tablelog);

            // 绑定TAB事件
            $('#tab-log a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                tablelog.bootstrapTable('refresh', {pageNumber: 1});
                return false;
            });
            //添加项目动态
            $(".btn-addlog").on("click",function () {
                var url=$(this).data('url');

                Fast.api.open(url,$(this).attr('title'),{callback: function () {
                        tablelog.bootstrapTable('refresh');
                }});
            });




            //adminlist
            var tableadmins = $("#tableadmins");
            // 初始化表格
            tableadmins.bootstrapTable({
                url: 'workteam/project/adminlist/project_id/'+project_id ,
                pk: 'id',
                sortName: 'id',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 1000,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: true,
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    return params
                },
                columns: [
                    {field: 'id', title: 'ID', operate: false},
                    {field: 'nickname', title: __('员工'), operate: false},
                    {field: 'task_count', title: __('任务'), operate: false,formatter: function (v,row,index) {
                            return v+"("+row.task_percentage+"%)";
                        }},
                    {field: 'task_hours', title: __('工时'),operate: false,formatter: function (v,row,index) {
                            return v+"("+row.hours_percentage+"%)";
                        }},
                    {field: 'delay_count', title: __('延误'),operate: false,formatter: function (v,row,index) {
                            return v+"("+row.delay_percentage+"%)";
                        }},

                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(tableadmins);

            var yjt_echats= Echarts.init(document.getElementById('yjt-echats'));



            // 指定图表的配置项和数据
            var legend = [],series = [];
            for (var i in dataEchart.data) {
                legend.push(i);
                series.push({
                    name: i,
                    type: 'line',

                    data: dataEchart.data[i]
                });
            }
            option = {
                title: {
                    text: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: legend
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: dataEchart.date
                },
                yAxis: {
                    type: 'value'
                },
                series: series,

            }
            // 使用刚指定的配置项和数据显示图表。
            yjt_echats.setOption(option);

        },
        print: function () {
            window.print();
            parent.layer.closeAll();

        },
        fileslist:function(){
            Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/project/fileslist/project_id/'+project_id,
                    add_url: 'workteam/project/filesadd/project_id/'+project_id,
                    edit_url: '',
                    del_url: 'workteam/project/filesdel/project_id/'+project_id,
                    multi_url: '',
                    table: ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('上传人'), operate: false},
                        {field: 'admin_id', title: __('上传人'), visible: false, addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'preview', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {field: 'filename', title: __('文件名称'), operate: false},
                        {field: 'url', title: __('文件地址'), formatter: Controller.api.formatter.url, visible: false},
                        {
                            field: 'create_time',
                            title: __('Createtime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {field: 'task.name', title: __('关联任务'), operate: false},

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //上传附件
            $("#c-third").on("change",function () {
                var url=$(this).val();
                if (url){
                    $(this).val("");
                    Fast.api.ajax({
                        url: $.fn.bootstrapTable.defaults.extend.add_url,
                        data: {url:url}
                    },function (data) {
                        table.bootstrapTable('refresh', {pageNumber: 1});
                    });
                }
            });
        },

        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    Fast.api.close(data);//在这里关闭当前弹窗
                });
                $(document).on("change", "#c-status", function(data,res){
                    if ($(this).val()>=2){
                        $(".complete_time").show();
                    }else{
                        $(".complete_time").hide();
                    }
                });
                $("#c-customer_id").selectPage({
                    eSelect: function (data) {
                        $("#c-customer_name").val(data.name);
                    },
                    eAjaxSuccess: function (data) {
                        data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                        data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                        return data;
                    }
                });
            },

            addRow:function (data) {
                var len = $('#table  tr').length;
                var lens = len - 2;
                var levelstr="";
                var groupdatastr="";
                levelList.forEach(function(value,i){
                    levelstr+='<option value="'+i+'">'+value+'</option>';
                })
                groupdata.forEach(function(value,i){
                    groupdatastr+='<option value="'+i+'">'+value+'</option>';
                })

                var tempTr =`<tr>
                    <td>
                    ${len}
                    </td>
                    <td >
                    <input type="text" name="task[${index}][name]" data-rule="required" >
                        </td>
                        <td class="start_time"  >
                        <input type="text" name="task[${index}][start_time]" data-rule="required" class="datetimepicker"  data-date-format="YYYY-MM-DD HH:mm" >
                        </td>
                        <td class="start_time">
                        <input type="text" name="task[${index}][end_time]" data-rule="required" class="datetimepicker"  >
                        </td>
                        <td class="plan_hours"><input type="text" name="task[${index}][plan_hours]" data-rule="required;integer"  ></td>
                        <td>
                        <select class="form-control selectpicker" data-rule="required" name="task[${index}][group_id]">
                        ${groupdatastr}
                        </select>
                        </td>
                        <td>
                        <input   data-source="workteam/common/selectpage/model/admin/type/all" data-field="nickname"
                class="form-control selectpage" name="task[${index}][admin_id]" type="text" value="" style="width: 110px" >
                        </td>
                        <td>
                        <input   data-source="workteam/common/selectpage/model/admin/type/all" data-field="nickname"
                class="form-control selectpage" name="task[${index}][admin_ids]" type="text" value="" data-multiple="true" style="width: 120px">
                        </td>
                        <td class="subtotal">
                        <select class="form-control selectpicker" data-rule="required" name="task[${index}][group_id]">
                        ${levelstr}
                        </select>
                        </td>
                        <td><textarea rows="2" class="remarks" name="task[${index}][content]" cols="20"></textarea></td>
                    <td>
                    <button type="button" class="btn btn-primary addRow" >${__('添加')}</button>
                    <button type="button" class="btn btn-default delRow" >${__('删除')}</button>
                    </td>
                    </tr>`;
                $('#table tbody tr:eq(' + lens + ')').after(tempTr);
                index++;
                $(function () {
                    Controller.api.bindevent();
                });
            },
            // 删除
            delRow:function(obj) {
                if ($('#table tbody tr').length == 1) {
                    alert(__("不能全部删除"));
                    return false;
                }
                $(obj).parent().parent().remove();
            },
            level: function (value, row, index,obj) {
                if (value==1) return '';
                var colorArr = ["primary", "success", "default", "info", "warning", "danger", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];

                var field = obj.field;
                if (typeof obj.customField !== 'undefined' && typeof row[obj.customField] !== 'undefined') {
                    field = obj.customField;
                }
                var color = index > -1 && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
                var icon='',display=Config.levelList[value];
                var html = '<span class="label label-' + color + '">' + display + '</span>';
                if (obj.operate != false) {
                    html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '">' + html + '</a>';
                }
                return html;
            },
            formatter: {
                thumb: function (value, row, index) {
                    var reg=/.*(\.png|\.jpg|\.jpeg|\.gif)$/;
                    if (reg.test(row.fullurl)) {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl  + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    } else {
                        return '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                    }
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                },
                filename: function (value, row, index) {
                    return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
                datetime: function (value, row, index) {
                    var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY/MM/DD HH:mm' : this.datetimeFormat;
                    if (isNaN(value)) {
                        return value ? Moment(value).format(datetimeFormat) : __('None');
                    } else {
                        return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
                    }
                },
            }
        }
    };
    return Controller;
});



// 删除
function delRow(obj) {

    if ($('#table tbody tr').length == 2) {
        alert(__("Can't delete"));
        return false;
    }
    $(obj).parent().parent().remove();
    setnums();
}

//设置序号
function setnums() {
    var len = $('#table tr').length;
    for (var i = 1; i < len - 1; i++) {
        $('#table tr:eq(' + i + ') td:first').text(i);
    }
}


function getNumValue(num) {
    if (!isNaN(num) && num != "") {
        num = parseFloat(num);
        var a = isPositiveInteger(num)
        if (a == false) {
            num = false;
        }
    } else {
        num = false;
    }
    return num;
}

function isInteger(obj) {
    return obj % 1 === 0
}

function isPositiveInteger(s) { //是否为正整数
    var re = /^[0-9]+$/;
    return re.test(s)
}







